package com.my.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.my.util.PackageUtil;

/**
 * Servlet implementation class DispacherServlet
 */
// http://127.0.0.1/TestWebAuth/user/add
public class DispatcherServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private static final String suffix = "Controller";
	private static final String packageToScan = "com.my.controller";
	Map<String, Object> mapping = new HashMap<String, Object>();
	
	@Override
	public void init() throws ServletException {
		super.init();
		System.out.println("DispatcherServlet.init()");
		PackageUtil.scan("com.my.controller", mapping);
		System.out.println(mapping);
	}

	@SuppressWarnings("unused")
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("getPathInfo:" + request.getPathInfo());
		System.out.println("getContextPath:" + request.getContextPath());
		System.out.println("getRequestURI:" + request.getRequestURI());
		System.out.println("getRequestURL:" + request.getRequestURL());
		System.out.println("getRemoteAddr:" + request.getRemoteAddr());
		System.out.println("getLocalAddr:" + request.getLocalAddr());
		
		System.out.println("------ServletContext------");
		
		ServletContext context = request.getServletContext();
		System.out.println("getContextPath" + context.getContextPath());
		System.out.println("getContextPath" + context.getRealPath("/"));// 服务器上的路径
		
		String contextPath = request.getContextPath();
		String requestURL = request.getRequestURL().toString();
		String url = requestURL.substring(requestURL.indexOf(contextPath) + contextPath.length() + 1);
		
		String className = null;
		String methodName = "index";
		String[] s = url.split("/");
		if(s.length == 1) {
			className = Character.toUpperCase(s[0].charAt(0)) + s[0].substring(1);
		} else if(s.length == 2) {
			className = Character.toUpperCase(s[0].charAt(0)) + s[0].substring(1);
			methodName = s[1];
		}
		dispather(className, methodName, request, response);
		
		int a = 1;
		
	}

	private void dispather(String className, String methodName, HttpServletRequest request, HttpServletResponse response) {
		Set<String> keys = mapping.keySet();
		for(String key: keys) {
			if(key.contains(className)) {
				Object target = mapping.get(key);
				try {
					Method m = target.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
					m.invoke(target, request, response);
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					e.printStackTrace();
					try {
						response.sendError(404);
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;
			}
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

	public static void main(String[] args) {
		String url = "user/add/";
		String[] s = url.split("/");
		if(s.length == 1) {
			System.out.println(s[0]);
		} else {
			System.out.println(s[0] + s[1]);
		}
	}
}
